package at.fhj.itm11.forum.data;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import at.fhj.itm11.forum.model.Rating;

@ApplicationScoped
public class RatingRepository {
	@Inject
	private EntityManager em;

	public Rating findById(Long id) {
		return em.find(Rating.class, id);
	}

	public Rating findRatingByRaterAndPost(Long userId, Long postId) {
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<Rating> criteria = cb.createQuery(Rating.class);
		Root<Rating> rating = criteria.from(Rating.class);

		criteria.select(rating).where(cb.equal(rating.get("user"), userId), cb.equal(rating.get("post"), postId));
		return em.createQuery(criteria).getSingleResult();
	}

	public Long findPositiveRatingCountForPost(Long postId) {
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<Long> criteria = cb.createQuery(Long.class);

		Root<Rating> rating = criteria.from(Rating.class);
		criteria.select(cb.count(rating));
		criteria.where(cb.equal(rating.get("post"), postId), cb.equal(rating.get("positiveRating"), true));
		return em.createQuery(criteria).getSingleResult();
	}

	public Long findNegativeRatingCountForPost(Long postId) {
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<Long> criteria = cb.createQuery(Long.class);

		Root<Rating> rating = criteria.from(Rating.class);
		criteria.select(cb.count(rating));
		criteria.where(cb.equal(rating.get("post"), postId),cb.equal(rating.get("positiveRating"), false));
		return em.createQuery(criteria).getSingleResult();
	}
}
